#################################################
##   author：侯勇                              ##
##   mail：yhouwork@163.com                    ##
##   Linux version：  Cent OS7                 ##
##   fastdfs version ：  V5.05                 ##
##   date ：2019/6/17                          ##
##   ps ：单机部署                             ##
#################################################

###################################################基础环境准备--文件夹###################################################
cd  /
if [ ! -d "/usr/local" ];
 then
 mkdir -p /usr/local
 fi
cd usr/local;
if [ ! -d "/tmp" ];
 then
 mkdir -p /tmp
 fi
cd /;
mkdir app;
mkdir -p /usr/local/nginx /var/log/nginx /var/temp/nginx /var/lock/nginx
mkdir -p /home/kevin/fastdfs/fastdfs-nginx-module/data-and-log/
mkdir -p /home/kevin/fastdfs/tracker/data-and-log
mkdir -p /usr/local/nginx/conf/conf.d
###################################################基础环境准备--插件环境###################################################
#wget
yum -y install  wget
#gcc
yum -y install  gcc
#perl
yum -y install perl*
# nginx依赖包
yum -y install  pcre pcre-devel
yum -y install  zlib zlib-devel
yum -y install  openssl openssl-devel
###################################################基础环境准备--安装包###################################################
echo -e "\033[34m   安装包下载 start  \033[0m"
cd /app;
#下载安装包 libfastcommon
if [ ! -e "/app/V1.0.7.tar.gz" ];
 then
    cd /app;
    echo -e "\033[34m 下载安装包  ： libfastcommon  \033[0m"
    wget https://github.com/happyfish100/libfastcommon/archive/V1.0.7.tar.gz
    cd -;
 fi
 #下载安装包 fastdfs
if [ ! -e "/app/V5.05.tar.gz" ];
 then
    cd /app;
    echo -e "\033[34m 下载安装包  ： fastdfs \033[0m"
    wget https://github.com/happyfish100/fastdfs/archive/V5.05.tar.gz
    cd -;
 fi

 #下载安装包 nginx
if [ ! -e "/app/nginx-1.11.8.tar.gz" ];
 then
    cd /app;
    echo -e "\033[34m 下载安装包  ： nginx  \033[0m"
    wget http://nginx.org/download/nginx-1.11.8.tar.gz
    cd -;
 fi


 #下载安装包 fastdfs-nginx-module
if [ ! -e "/app/fastdfs-nginx-module_v1.16.tar.gz" ];
 then
    cd /app;
    echo -e "\033[34m 下载安装包  ： fastdfs-nginx-module \033[0m"
    wget http://jaist.dl.sourceforge.NET/project/fastdfs/FastDFS%20Nginx%20Module%20Source%20Code/fastdfs-nginx-module_v1.16.tar.gz
    cd -;
 fi
echo -e "\033[34m   安装包下载 end  \033[0m"
###################################################libfastcommon 安装###################################################
cd /app;
tar -zxvf V1.0.7.tar.gz
cd libfastcommon-1.0.7
./make.sh
./make.sh install
ln -s /usr/lib64/libfastcommon.so /usr/local/lib/libfastcommon.so
ln -s /usr/lib64/libfastcommon.so /usr/lib/libfastcommon.so
###################################################fastdfs 安装###################################################
cd /app;
tar -zxvf V5.05.tar.gz
cd fastdfs-5.05
./make.sh
./make.sh install
ln -s /usr/lib64/libfdfsclient.so /usr/local/lib/libfdfsclient.so
ln -s /usr/lib64/libfdfsclient.so /usr/lib/libfdfsclient.so
###################################################fastdfs-nginx-module 安装###################################################
cd /app;
tar -zxvf fastdfs-nginx-module_v1.16.tar.gz
cp /app/fastdfs-nginx-module/src/mod_fastdfs.conf /etc/fdfs
###################################################nginx 安装###################################################
cd /app;
tar -zxvf nginx-1.11.8.tar.gz
cd nginx-1.11.8
###################################################公共变量开始###################################################
ip=$(ip a show dev ens33|grep -w inet|awk '{print $2}'|sed 's/\/.*//');
store_path="\/home\/kevin\/fastdfs\/storage\/images-data"
store_path1="/home/kevin/fastdfs/storage/images-data"
#声明行
#所在行
num=;
#插入行
numa=0;
#默认行为空
nullnum=;
###################################################公共变量结束###################################################
###################################################配置文件开始###################################################

#####需要修改的文件
mycnfdir=/etc/fdfs/storage.conf
cd /etc/fdfs;
rm -rf storage.conf
cp /etc/fdfs/storage.conf.sample /etc/fdfs/storage.conf
#获取base_path所在行
num=$(grep -n '^base\_path\=.*'  $mycnfdir| awk -F ':' '{print $1}')
if  [  !  "$num" = ""    ] ;then
    echo -e "\033[34m =================》$mycnfdir  base_path此变量是存在的 进行修改  $num《=================  \033[0m"
    #此变量是存在的 进行修改
    sed -i 's/base\_path\=.*/base\_path\=\/home\/kevin\/fastdfs\/storage/g' $mycnfdir
fi
num=$nullnum;

#store_path修改
for((i=0;i<=2;i++));
do
    num=$(grep -n '^store\_path'$i'\=.*'  $mycnfdir| awk -F ':' '{print $1}')
    if  [  !  "$num" = ""    ] ;then
        echo -e "\033[34m =================》$mycnfdir  store_path此变量是存在的 进行修改  $num《=================  \033[0m"
        #此变量是存在的 进行修改
        sed -i 's/store\_path'$i'\=.*/store\_path'$i'\='$store_path$i'/g' $mycnfdir
    else
        #此变量不存在
        #获取需要插入的位置
        j=`expr $i - 1`
        num=$(grep -n '^store\_path'$j'\=.*'  $mycnfdir| awk -F ':' '{print $1}')
        echo -e "\033[34m =================》$mycnfdir  store_path此变量不存在  进行插入  $num《=================  \033[0m"
        numa=$num"a"
        sed -i $numa' store\_path'$i'\='$store_path$i $mycnfdir ;
    fi
    num=$nullnum;
    mkdir -p $store_path1$i"/data"
done

num=$(grep -n '^tracker\_server\=.*'  $mycnfdir| awk -F ':' '{print $1}')
if  [  !  "$num" = ""    ] ;then
    echo -e "\033[34m =================》$mycnfdir  tracker_server此变量是存在的 进行修改  $num《=================  \033[0m"
    #此变量是存在的 进行修改
    sed -i 's/tracker\_server\=.*/tracker\_server\='$ip':22122/g' $mycnfdir
fi
num=$nullnum;

#####需要修改的文件
mycnfdir=/etc/fdfs/tracker.conf
cd /etc/fdfs;
rm -rf tracker.conf
cp /etc/fdfs/tracker.conf.sample /etc/fdfs/tracker.conf

num=$(grep -n '^base\_path\=.*'  $mycnfdir| awk -F ':' '{print $1}')
if  [  !  "$num" = ""    ] ;then
    echo -e "\033[34m =================》 $mycnfdir  base_path此变量是存在的 进行修改  $num《=================  \033[0m"
    #此变量是存在的 进行修改
    sed -i 's/base\_path\=.*/base\_path\=\/home\/kevin\/fastdfs\/tracker\/data\-and\-log/g' $mycnfdir
fi
num=$nullnum;

#####需要修改的文件
mycnfdir=/app/fastdfs-nginx-module/src/config
num=$(grep -n '^CORE\_INCS\=.*'  $mycnfdir| awk -F ':' '{print $1}')
if  [  !  "$num" = ""    ] ;then
    echo -e "\033[34m =================》$mycnfdir CORE_INCS 此变量是存在的 进行修改  $num《=================  \033[0m"
    #此变量是存在的 进行修改
    sed -i 's/CORE\_INCS\=.*/CORE\_INCS\=\"\$CORE_INCS \/usr\/include\/fastdfs\ \/usr\/include\/fastcommon\/\"/g' $mycnfdir
fi
num=$nullnum;
cp /app/fastdfs-5.05/conf/http.conf /etc/fdfs
cp /app/fastdfs-5.05/conf/mime.types /etc/fdfs

cd /app/nginx-1.11.8
#修改CORE_INCS 完成后再进行模组添加否则会报致命错误
./configure --add-module=/app/fastdfs-nginx-module/src
make
make install
pkill -9 nginx



#####需要修改的文件
mycnfdir=/etc/fdfs/mod_fastdfs.conf
num=$(grep -n '^base\_path\=.*'  $mycnfdir| awk -F ':' '{print $1}')
if  [  !  "$num" = ""    ] ;then
    echo -e "\033[34m =================》$mycnfdir base_path此变量是存在的 进行修改  $num《=================  \033[0m"
    #此变量是存在的 进行修改
    sed -i 's/base\_path\=.*/base\_path\=\/home\/kevin\/fastdfs\/tracker\/data\-and\-log/g' $mycnfdir
fi
num=$nullnum;

num=$(grep -n '^url\_have\_group\_name\ \=.*'  $mycnfdir| awk -F ':' '{print $1}')
if  [  !  "$num" = ""    ] ;then
    echo -e "\033[34m =================》$mycnfdir url_have_group_name 此变量是存在的 进行修改  $num《=================  \033[0m"
    #此变量是存在的 进行修改
    sed -i 's/url\_have\_group\_name\ \=.*/url\_have\_group\_name\ \=\ true/g' $mycnfdir
fi
num=$nullnum;

for((i=0;i<=2;i++));
do
    num=$(grep -n '^store\_path'$i'\=.*'  $mycnfdir| awk -F ':' '{print $1}')
    if  [  !  "$num" = ""    ] ;then
        echo -e "\033[34m =================》$mycnfdir store_path$i 此变量是存在的 进行修改  $num《=================  \033[0m"
        #此变量是存在的 进行修改
        sed -i 's/store\_path'$i'\=.*/store\_path'$i'\='$store_path$i'/g' $mycnfdir
    else
        #此变量不存在
        #获取需要插入的位置
        j=`expr $i - 1`
        num=$(grep -n '^store\_path'$j'\=.*'  $mycnfdir| awk -F ':' '{print $1}')
        echo -e "\033[34m =================》$mycnfdir store_path$i 此变量不存在  进行插入  $num《=================  \033[0m"
        numa=$num"a"
        sed -i $numa'store\_path'$i'\='$store_path$i $mycnfdir ;
    fi
    num=$nullnum;
done

num=$(grep -n '^tracker\_server\=.*'  $mycnfdir| awk -F ':' '{print $1}')
if  [  !  "$num" = ""    ] ;then
    echo -e "\033[34m =================》$mycnfdir tracker_server此变量是存在的 进行修改  $num《=================  \033[0m"
    #此变量是存在的 进行修改
    sed -i 's/.*tracker\_server\=.*/tracker\_server\='$ip':22122/g' $mycnfdir
fi
num=$nullnum;


#####需要修改的文件
mycnfdir=/usr/local/nginx/conf/nginx.conf

num=$(grep -n '^http\ {'  $mycnfdir| awk -F ':' '{print $1}')
numa=$num"a"
sed -i $numa' include\ \/usr\/local\/nginx\/conf\/conf\.d\/\*\.conf\;' $mycnfdir ;


num=$nullnum;

echo "server {
            listen       8888;
            listen       [::]:8888;
            server_name  $ip;
            location ~/group[0-9]/ {
                ngx_fastdfs_module;
            }
            error_page   500 502 503 504  /50x.html;
            location = /50x.html {
            root   html;
            }
        }" > /usr/local/nginx/conf/conf.d/fdfs.conf
###################################################配置文件结束###################################################
###################################################启动服务开始###################################################
#启动 tracker 服务
/usr/bin/fdfs_trackerd /etc/fdfs/tracker.conf
echo -e "\033[34m =================》tracker 启动完成《=================  \033[0m"

#启动 storage 服务
/usr/bin/fdfs_storaged /etc/fdfs/storage.conf
echo -e "\033[34m =================》storage 启动完成《=================  \033[0m"

#启动 nginx 服务
/usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
echo -e "\033[34m =================》nginx 启动完成《=================  \033[0m"
###################################################启动服务结束###################################################